<?php

namespace Biz\model\trade;

use Biz\model\ShopBasicInfo;
use Biz\service\Goods;
use Doraemon\service\User;
use Medoo\Medoo;

/**
 * 开团记录
 */
class GroupJoin extends \Doraemon\pockets\datebase\BaseDbModel
{

    const _CONFIG_FILE_ = 'db_shop';

    const _CONFIG_SELECT_ = 'db_trade';

    const _DATABASE_ = 'shop_trade';

    const _TABLE_NAME = 'group_join';

    //拼团状态
    const STATUS_WAIT_PAY = 0;
    const STATUS_ING = 1;
    const STATUS_SUCCESS = 2;
    const STATUS_FAIL = 3;
    const STATUS_FAIL_OUT_JOIN = 4;
    const STATUS_DEL = 9;
    const STATUS_DESC_MAP = [
        self::STATUS_WAIT_PAY => '未开始',
        self::STATUS_ING => '进行中',
        self::STATUS_SUCCESS => '拼团成功',
        self::STATUS_FAIL => '拼团失败',
        self::STATUS_FAIL_OUT_JOIN => '支付晚了',
    ];
    const STATUS_DESC_ADM_MAP = [
        self::STATUS_WAIT_PAY => '未开始',
        self::STATUS_ING => '进行中',
        self::STATUS_SUCCESS => '拼团成功',
        self::STATUS_FAIL => '拼团失败',
    ];
    //失败原因
    const FAIL_REASON_NOT_ENOUGH_STOCK = 1; //真实库存已为0
    const FAIL_REASON_OVER_TIME = 2; //超时未凑够人数
    const FAIL_REASON_MAP = [
        self::FAIL_REASON_NOT_ENOUGH_STOCK => '真实库存已为0',
        self::FAIL_REASON_OVER_TIME => '超时未凑够人数',
    ];

    public static function addData($group_id, $type, $member, $uid, $shop_id, $goods_id, $group_buy_price, $group_cycle, $end_time)
    {
        $addData = [
            'group_id'  => $group_id,
            'type'      => $type,
            'member'    => $member,
            'join_member' => 0,
            'uid'       => $uid,
            'shop_id'   => $shop_id,
            'goods_id'  => $goods_id,
            'group_buy_price' => $group_buy_price,
            'group_cycle' => $group_cycle,
            'end_time' => $end_time,
            'status'    => self::STATUS_WAIT_PAY,
        ];
        $rs = self::insert($addData);
        return self::id();
    }

    public static function getCancelGroup()
    {
        $where = [
            'end_time[<]' => date('Y-m-d H:i:s'),
            'status'    => [
                self::STATUS_ING,
            ],
        ];
        return self::select(['id'], $where);

    }

    public static function getCreateCount($uid, $group_id = 0)
    {
        $where = [
            'uid'       => $uid,
            'status'    => [
                self::STATUS_ING
            ],
        ];
        if ($group_id > 0) {
            $where['group_id'] = $group_id;
        }
        return self::count($where);
    }

    public static function getWaitCancel($groupId)
    {
        $rs = self::select(
            ['id', 'member'], Medoo::raw(' where group_id=:group_id and status=:status', [
            ':group_id' => $groupId,
            ':status' => self::STATUS_ING,
        ]));
//        var_dump(self::log());
        return $rs;
    }


    public static function getUserListAndCount($where)
    {
        $list = self::getUserList($where);
        unset($where['LIMIT']);
        $total = self::getUserCount($where);
        return compact('list', 'total');
    }
    public static function getUserList($where)
    {
        if (!isset($where['ORDER'])) {
            $where['ORDER'] = [
                'id' => 'DESC'
            ];
        }
        if (empty($where['AND'])) {
            unset($where['AND']);
        }

        $list = self::select("*", $where);
        var_dump(self::last());
        $goods_ids = empty($list) ? [] : array_column($list, 'goods_id');
        $shop_ids = empty($list) ? [] : array_column($list, 'shop_id');
        $group_join_id = empty($list) ? [] : array_column($list, 'id');
        $goodsInfo = Goods::Goods($goods_ids);
        $shopInfo = ShopBasicInfo::getShopInfoList($shop_ids);
        $joinMember = GroupJoinMember::select(['group_join_id', 'count' => Medoo::raw("count(*)")], [
            'AND' => [
                'uid' => 0,
                'group_join_id' => $group_join_id,
            ],
            'GROUP' => ['group_join_id']
        ]);
        $joinMap = empty($joinMember) ? [] : array_column($joinMember, 'count', 'group_join_id');


        foreach ($list as $key => $v) {
            $sys_time = time();
            $end_time = strtotime($v['end_time']);
            $last_time = $end_time - $sys_time;
            $last_time = $last_time > 0 ? $last_time : 0;
            $list[$key]['last_time'] = $last_time;
//            if ($v['status'] == GroupJoin::STATUS_ING && $last_time == 0) {
//                $list[$key]['reason'] = GroupJoin::FAIL_REASON_OVER_TIME;
//                $list[$key]['status'] = GroupJoin::STATUS_FAIL;
//            }
            $userInfo = User::getUserInfo($v['uid']);
            $list[$key]['nick'] = isset($userInfo['nick']) ? $userInfo['nick'] : '';
            $list[$key]['goods_title'] = isset($goodsInfo[$v['goods_id']]) ? $goodsInfo[$v['goods_id']]['title'] : '';
            $list[$key]['shop_name'] = isset($shopInfo[$v['shop_id']]) ? $shopInfo[$v['shop_id']]['name'] : '';
            $list[$key]['desc'] = isset($joinMap[$v['id']]) ? '(后台成团)' : '';
        }
        return $list;
    }
    public static function getUserCount($where)
    {
        if (isset($where['LIMIT'])) {
            unset($where['LIMIT']);
        }
        if (isset($where['ORDER'])) {
            unset($where['ORDER']);
        }
        $rs = self::count('*',$where);
        return $rs;
    }
}

